<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
<title>THE BOOST MPL LIBRARY: Implementing Addition and Subtraction</title>
<link rel="stylesheet" href="../style.css" type="text/css" />
</head>
<body class="docframe">
<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./representing-quantities.html" class="navigation-link">Prev</a>&nbsp;<a href="./implementing.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./representing-quantities.html" class="navigation-link">Back</a>&nbsp;<a href="./implementing.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
<td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./tutorial-metafunctions.html" class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <a href="./dimensional-analysis.html" class="navigation-link">Dimensional Analysis</a> / <a href="./implementing-addition-and.html" class="navigation-link">Implementing Addition and Subtraction</a></td>
</tr></table><div class="header-separator"></div>
<div class="section" id="implementing-addition-and">
<h1><a class="toc-backref" href="./dimensional-analysis.html#id44" name="implementing-addition-and">Implementing Addition and Subtraction</a></h1>
<p>We can now easily write the rules for addition and subtraction,
since the dimensions of the arguments must always match.</p>
<pre class="literal-block">
template &lt;class T, class D&gt;
quantity&lt;T,D&gt;
operator+(quantity&lt;T,D&gt; x, quantity&lt;T,D&gt; y)
{
  return quantity&lt;T,D&gt;(x.value() + y.value());
}

template &lt;class T, class D&gt;
quantity&lt;T,D&gt;
operator-(quantity&lt;T,D&gt; x, quantity&lt;T,D&gt; y)
{
  return quantity&lt;T,D&gt;(x.value() - y.value());
}
</pre>
<!-- @ example.append('void test1() {') # function prologue -->
<p>These operators enable us to write code like:</p>
<pre class="literal-block">
quantity&lt;float,<strong>length</strong>&gt; len1( 1.0f );
quantity&lt;float,<strong>length</strong>&gt; len2( 2.0f );

len1 = len1 + len2;   // OK
</pre>
<!-- @litre_translator.line_offset -= 7 -->
<p>but prevent us from trying to add incompatible dimensions:</p>
<pre class="literal-block">
len1 = len2 + quantity&lt;float,<strong>mass</strong>&gt;( 3.7f ); // <strong>error</strong>
</pre>
<!-- @ stack[-1].append('}') # last 2 examples together
compile('all', pop = 1, expect_error = True)

stack[-1].append('}') # just the prior example
compile('all', pop = None) -->
</div>

<div class="footer-separator"></div>
<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./representing-quantities.html" class="navigation-link">Prev</a>&nbsp;<a href="./implementing.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./representing-quantities.html" class="navigation-link">Back</a>&nbsp;<a href="./implementing.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
</tr></table></body>
</html>
